import 'package:boss/provider/table_provider.dart';
import 'package:boss/ui/page/tableManagement/page/areaManagement/page/areaDetail/provider/area_detail_provider.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class AreaDetailFooter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<AreaDetailProvider>(
      builder: (context, areaDetailProvider, _) {
        return FixedBottomBtn(
          isShowShadow: false,
          title: '保 存',
          onTap: () => areaDetailProvider.isUpdate ? _updateStoreArea(context) : _plusStoreArea(context),
        );
      },
    );
  }

  _plusStoreArea(BuildContext context) async {
    var areaDetailProvider = Provider.of<AreaDetailProvider>(
      context,
      listen: false,
    );
    var tableProvider = Provider.of<TableProvider>(
      context,
      listen: false,
    );

    var result = await tableProvider.plusStoreArea(areaDetailProvider.storeArea);

    if (result) Navigator.pop(context);
  }

  _updateStoreArea(BuildContext context) async {
    var areaDetailProvider = Provider.of<AreaDetailProvider>(
      context,
      listen: false,
    );
    var tableProvider = Provider.of<TableProvider>(
      context,
      listen: false,
    );

    var result = await tableProvider.updateStoreArea(areaDetailProvider.storeArea);

    if (result) Navigator.pop(context);
  }
}
